/*****************************************************************************
 * kal_rtthread_mutex.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "kal/source/kal_rthread/kal_rtthread.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "kal.mutex"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/
typedef struct {
    plum_s8          name[PLUM_KAL_MUTEX_NAME_MAX];
    rt_mutex_t       hdl;
    struct list_head list;
} kal_mutex_t;
/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/
PLUM_PRIVATE
kal_mutex_t mutex_head;
/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
kal_mutex_t *kal_rtthread_mutex_register(plum_s8 *name)
{
    kal_mutex_t *node = plum_kal_mem_malloc(sizeof(kal_mutex_t));
    if (node) {
        memset(node, 0, sizeof(kal_mutex_t));
        snprintf(node->name, sizeof(node->name), "%s", name);

        if (!mutex_head.list.next) {
            LIST_HEAD_INIT(&mutex_head.list);
        }

        list_add(&node->list, &mutex_head.list);
    }

    return (node);
}

PLUM_PRIVATE
plum_void kal_rtthread_mutex_unregister(kal_mutex_t *mtx)
{
    list_del(&mtx->list);
    plum_kal_mem_free(mtx);
}

PLUM_PRIVATE
kal_mutex_t *kal_rtthread_mutex_find(plum_s8 *name)
{
    struct list_head *pos;
    list_for_each(pos, &sem_head.list)
    {
        kal_mutex_t *node = list_entry(pos, kal_mutex_t, list);
        if (strncmp(node->name, name, strlen(name)) == 0) {
            return (node);
        }
    }

    return (plum_null);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_kal_mutex_create(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("the sem name is null");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        kal_mutex_t *mtx = kal_rtthread_mutex_register(name);
        if (!mtx) {
            rc = PLUM_ECODE_EMEM;
            LOG_E("kal_rtthread_mutex_register err,rc : %d", rc);
            break;
        }

        node->hdl = rt_mutex_create(name, 1);
        if (!node->hdl) {
            rc = PLUM_ECODE_EIO;
            LOG_E("rt_mutex_create err,rc : %d", rc);
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_mutex_delete(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("the sem name is null");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        kal_mutex_t *mtx = kal_rtthread_mutex_find(name);
        if (!mtx) {
            LOG_E("not find the mutex,named : %s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = rt_mutex_delete(mtx->hdl);
        if (rc) {
            LOG_E("rt_mutex_delete err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        kal_rtthread_mutex_unregister(mtx);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_mutex_lock(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("the sem name is null");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        kal_mutex_t *mtx = kal_rtthread_mutex_find(name);
        if (!mtx) {
            LOG_E("not find the mutex,named : %s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = rt_mutex_release(mtx->hdl);
        if (rc) {
            LOG_E("rt_mutex_release err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_mutex_unlock(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("the sem name is null");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        kal_mutex_t *mtx = kal_rtthread_mutex_find(name);
        if (!mtx) {
            LOG_E("not find the mutex,named : %s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = rt_mutex_take(mtx->hdl, 0);
        if (rc) {
            LOG_E("rt_mutex_take err,rc : %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_void plum_kal_mutex_show(PLUM_CONST plum_s8 *name)
{
    if (name) {
        kal_mutex_t *node = kal_rtthread_mutex_find(name);
        if (node) {
            LOG_D("mutex [%p] named:%s", node->hdl, node->name);
        }
    }
    else {
        struct list_head *pos;
        list_for_each(pos, &thread_head.list)
        {
            kal_mutex_t *node = list_entry(pos, kal_mutex_t, list);
            LOG_D("mutex [%p] named:%s", node->hdl, node->name);
        }
    }
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
